<template>
  <div class="app-container" style="padding: 10px">
    <el-card class="box-card" shadow="false">
      <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="80px">
        <el-form-item label="ID" prop="id" v-if="false">
          <el-input
            v-model="queryParams.id"
            placeholder="请输入考试任务ID"
            clearable
          />
        </el-form-item>
        <el-form-item label="考试状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择考试状态" clearable>
            <el-option v-for="dict in dict.type.exam_test_task_status"
                       :key="dict.value"
                       :label="dict.label"
                       :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input
            v-model="queryParams.userName"
            placeholder="请输入用户名"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery"> 搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery"> 重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="small"
            @click="handleAdd"
          >
            添加考试任务
          </el-button>
        </el-col>
        <right-toolbar :show-search.sync="showSearch" @queryTable="getList"/>
      </el-row>

      <el-table v-loading="loading" :data="dataList" border>
        <el-table-column label="ID" prop="id" width="200px" v-if="false"/>
        <el-table-column label="考试标题" prop="taskInfoTitle"/>
        <el-table-column label="试卷名称" prop="testPaperName" sortable>
          <template slot-scope="scope">
            {{ scope.row.testPaperId ? "《" + scope.row.testPaperName + "》" : "自定义试题" }}
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="userName" width="80px" sortable align="center"/>
        <el-table-column label="状态" width="80px" align="center">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.exam_test_task_status" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="考试时间" width="260px" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.status === '0'">无</span>
            <div v-else>
              <div>
                <span>开始时间：{{ scope.row.startTime }}</span>
              </div>
              <div>
                <span>结束时间：{{ scope.row.endTime }}</span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="试卷总分" prop="testPaperFraction" width="80px" align="center"/>
        <el-table-column label="得分" prop="score" sortable width="80px" align="center">
          <template slot-scope="scope">
            {{ scope.row.status === '0' ? '无' : scope.row.score }}
          </template>
        </el-table-column>
        <el-table-column label="纠正得分" prop="rectifyScore" sortable width="100px" align="center">
          <template slot-scope="scope">
            {{ scope.row.status === '0' ? '无' : scope.row.rectifyScore }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="180px">
          <template slot-scope="scope">
            <el-button
              icon="el-icon-edit-outline"
              size="mini"
              type="text"
              :disabled="scope.row.status === '0'"
              @click="handleScore(scope.row.id)">
              评分
            </el-button>
            <el-button
              icon="el-icon-refresh"
              size="mini"
              type="text"
              :disabled="scope.row.status !== '1'"
              @click="handleRecycle(scope.row.id)">
              重考
            </el-button>
            <el-button
              icon="el-icon-delete"
              size="mini"
              type="text"
              :disabled="scope.row.status !== '0'"
              @click="handleDelete(scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :limit.sync="queryParams.pageSize"
        :page.sync="queryParams.pageNum"
        :total="total"
        @pagination="getList"
      />

      <el-dialog :visible.sync="scoreShow" title="考试评分" v-if="scoreShow">
        <grade :id="infoId" @close="scoreShowClose"/>
      </el-dialog>

      <el-dialog :visible.sync="show" title="添加考试任务" size="30%">
        <div>
          <el-table v-loading="userLoading" size="mini" :data="userList" @selection-change="handleSelectionChange"
                    style="max-height: 400px" border>
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="工号" prop="userName"/>
            <el-table-column label="姓名" prop="nickName"/>
            <el-table-column label="手机号码" prop="phonenumber"/>
          </el-table>
        </div>
        <div style="text-align: right; margin-top: 20px">
          <el-button @click="cancel">
            取 消
          </el-button>
          <el-button type="primary" @click="submit">
            保 存
          </el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import {recycleExam, taskAdd, testTaskDelete, testTaskList} from "@/api/exam/testTask";
import Grade from './grade.vue'
import {listUser} from "@/api/app/user";

export default {
  name: 'index',
  dicts: ['exam_test_task_status'],
  components: {Grade},
  data() {
    return {
      loading: false,
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      dataList: [],
      scoreShow: false,
      infoId: null,
      show: false,
      userLoading: false,
      userList: [],
      params: {}
    }
  },
  created() {
    this.queryParams.taskInfoId = this.$route.query.taskInfoId
    this.handleQuery()
  },
  methods: {
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10
      }
      this.getList()
    },
    getList() {
      this.loading = true
      testTaskList(this.queryParams).then(res => {
        this.dataList = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    handleDelete(id) {
      this.$confirm('是否删除该考试任务?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        testTaskDelete(id).then(() => {
          this.handleQuery()
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        })
      })
    },
    handleScore(id) {
      this.infoId = id
      this.scoreShow = true
    },
    handleRecycle(id) {
      this.$confirm('考试任务重考?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        recycleExam(id).then(res => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.getList()
        })
      })
    },
    scoreShowClose() {
      this.scoreShow = false
      this.handleQuery()
    },
    handleAdd() {
      this.getUserList()
      this.show = true
    },
    getUserList() {
      this.userLoading = true
      listUser({
        pageNum: 1,
        pageSize: 9999,
        userType: 'front_user'
      }).then(
        (response) => {
          this.userList = response.rows;
          this.userLoading = false
        }
      );
    },
    handleSelectionChange(selection) {
      this.params.userIds = selection.map(item => item.userId).toString()
    },
    cancel() {
      this.show = false
      this.getList()
    },
    submit() {
      this.params.taskInfoId = this.queryParams.taskInfoId
      taskAdd(this.params).then(res => {
        this.show = false
        this.getList()
      })
    }
  }
}
</script>
